package com.example.isoftservice.controller;

import com.example.isoftservice.pojos.News;
import com.example.isoftservice.service.NewsService;
import com.example.isoftservice.util.Result;
import com.example.isoftservice.util.logs.HoneyLogs;
import com.example.isoftservice.util.logs.LogTypes;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


@CrossOrigin
@RestController
@RequestMapping("/news")
@Slf4j
public class NewsController {


    //动态获取配置文件中的文件路径
    @Value("${filePath}")
    private String filePath;

    @Value("${ip}")
    private String ip;

    @Value("${port}")
    private String port;

    @Resource
    private NewsService newsService;

    //查询通告列表
    @RequestMapping(value = "/newsList",method = RequestMethod.GET)
    public Result<Map<String,Object>> searchNewsList(@RequestParam(name = "pageNo",defaultValue = "1") Integer pageNo,
                                                     @RequestParam(name = "pageSize",defaultValue = "5")Integer pageSize) {
        System.out.println("pageNo:"+pageNo+"pageSize:"+pageSize);
        Result<Map<String,Object>> result = new Result<>();
        result.setMessage("查询成功");
        result.setSuccess(true);
        result.setCode(0);
        result.setResult(newsService.searchNewsList(pageNo,pageSize));
        return result;
    }

    //按id查通告
    @RequestMapping(value = "/searchNewsById",method = RequestMethod.GET)
    public Result<News> searchNewsById( Integer newsId) {
        Result<News> result = new Result<>();
        result.setMessage("查询成功");
        result.setSuccess(true);
        result.setCode(0);
        result.setResult(newsService.searchNewsById(newsId));
        return result;
    }


    //新增通告
    @HoneyLogs(operation = "通告",type = LogTypes.ADD)
    @RequestMapping(value = "/addNews", method = RequestMethod.POST)
    public Result<Object> addNews(@RequestBody News news) {
        System.out.println(news);
        Result<Object> result = new Result<>();
        result.setMessage("添加成功!");
        result.setSuccess(newsService.addNews(news));
        return result;
    }


    //批量(单个)删除通告
    @HoneyLogs(operation = "通告",type = LogTypes.DELETE)
    @RequestMapping(value = "/removeNews",method = RequestMethod.POST)
    public  Result<Object> removeNews(@RequestBody News news) {
        Result<Object> result = new Result<>();
        result.setMessage("删除成功!");
        result.setSuccess(true);
        result.setCode(0);
        result.setSuccess(newsService.removeNews(news.ids));
        return result;
    }

    //更新通告
    @HoneyLogs(operation = "通告",type = LogTypes.UPDATE)
    @RequestMapping(value = "/modifyNews",method = RequestMethod.POST)
    public  Result<Object> modifyNews(@RequestBody News news) {
        Result<Object> result = new Result<>();
        result.setMessage("修改成功!");
        result.setSuccess(true);
        result.setCode(0);
        result.setSuccess(newsService.modifyNews(news));
        return result;
    }


    //提交图片下载的url
    @HoneyLogs(operation = "通告",type = LogTypes.FILE)
    @RequestMapping("/uploadFile")
    public Map<String,Object> uploadFile(@RequestParam("wangeditor-uploaded-image") MultipartFile multipartFile) {
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> data = new HashMap<String, Object>();
        String fileName =  multipartFile.getOriginalFilename();
        String sname = fileName.substring(fileName.lastIndexOf("."));
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String timeStamp = simpleDateFormat.format(new Date());
        String newName = timeStamp + sname;

        //动态获取配置文件的路径
        File uploadFile = new File(filePath + newName);
        //上传前对上传路径进行验证
        if (! uploadFile.exists()) {
            uploadFile.mkdirs();
        }
        try {
            multipartFile.transferTo(uploadFile);
            data.put("url","http://"+ ip + ":" + port + "/download?url=" + filePath + newName);
            data.put("alt",newName);
            data.put("href","http://"+ ip + ":" + port + "/download?url=" + filePath + newName);
            map.put("errno",0);
            map.put("data",data);
        } catch (IOException e) {
            e.printStackTrace();
            map.put("error",1);
            map.put("message","失败信息");
        }

        return map;
    }
    }




